package com.bite.friend.controller.user;

import com.bite.common.core.constants.HttpConstants;
import com.bite.common.core.controller.BaseController;
import com.bite.common.core.domain.R;
import com.bite.common.core.domain.vo.LoginUserVO;
import com.bite.friend.domain.user.dto.UserUpdateDTO;
import com.bite.friend.domain.user.vo.UserVO;
import com.bite.friend.service.user.IUserService;
import com.bite.friend.domain.user.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping ("/user")
public class UserController extends BaseController {
    @Autowired
    public IUserService userService;
    //user/setCode  实现验证码，携带手机号不是很安全，所以使用post
    @PostMapping("/sendCode")
    public R<Void> sendCode(@RequestBody UserDTO userDTO){
        return toR(userService.sendCode(userDTO));
    }
    //code//login
    @PostMapping("/code/login")
    public R<String > codeLogin(@RequestBody UserDTO userDTO){
        //登录逻辑:根据用户输入的验证码和之前为这个用户生成的验证码进行比较，相同则登录成功，不同则失败
    return  R.ok(userService.codeLogin(userDTO.getPhone(),userDTO.getCode()));
    }
    //接口地址/system/sysUser/logout
    @DeleteMapping("logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return toR(userService.logout(token));
    }
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return userService.info(token);
    }

        @GetMapping("/detail")
        public R<UserVO> detail() {
            return R.ok(userService.detail());
        }

        @PutMapping("/edit")
        public R<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
            return toR(userService.edit(userUpdateDTO));
        }
        //更新这个字段
        @PutMapping("/head-image/update")
        public R<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
            return toR(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
        }
    }
